package org.jks.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * Ftp 连接工具
 * @author turan
 *
 */
public class FtpUtil {
	private FTPClient ftpClient = new FTPClient();
	private String ip = "192.168.0.103";
	private int port = 21;
	private String username = "turan";
	private String password = "turan";
	private String encoding = "GBK";
	
	public FtpUtil(String ip, int port, String username, String password){
		this.ip = ip;
		this.port = port;
		this.username = username;
		this.password = password;
	}
	
	public boolean connect() {
		boolean ret = false;
		try {
			int reply;
			ftpClient.connect(ip, port);
			ftpClient.login(username, password);
			ftpClient.setControlEncoding(encoding);
			reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
			} else {
				ret = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public void close() {
		try {
			ftpClient.logout();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (ftpClient.isConnected()) {
			try {
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean upload(String path, String filename, String localpath) {
		if(!ftpClient.isConnected()){
			return false;
		}
		File file = new File(localpath);
		if(!file.exists()){
			return false;
		}
		boolean result = false;
		try {
			// 转移工作目录至指定目录下
			boolean change = ftpClient.changeWorkingDirectory(path);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			if (change) {
				FileInputStream input = new FileInputStream(file);
				result = ftpClient.storeFile(
						new String(filename.getBytes(encoding), "ISO-8859-1"),
						input);
				input.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return result;
	}

	public boolean download(String remotePath, String filename,
			String localPath) {
		if(!ftpClient.isConnected()){
			return false;
		}
		boolean result = false;
		try {
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.changeWorkingDirectory(new String(remotePath
					.getBytes(encoding), "ISO-8859-1"));
			FTPFile[] fs = ftpClient.listFiles();
			for (FTPFile ff : fs) {
				if (ff.getName().equals(filename)) {
					File localFile = new File(localPath + "/" + ff.getName());
					OutputStream is = new FileOutputStream(localFile);
					result = ftpClient.retrieveFile(ff.getName(), is);
					is.close();
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return result;
	}

	public static void main(String args[]) {
		FtpUtil ftpUtil = new FtpUtil("192.168.0.103", 21, "turan", "turan");
		boolean ret = false;
		ret = ftpUtil.connect();
		if(ret){
			System.out.println("连接成功");
		}
		ret = ftpUtil.upload("/", "中文.properties", "D:\\中文.properties");
		if(ret){
			System.out.println("上传文件成功");
		}
		ret = ftpUtil.download("/", "spring mvc.docx", "D:/");
		if(ret){
			System.out.println("下载文件成功");
		}
		
		ftpUtil.close();
	}
}
